<?php

class CouponApp extends MallbaseApp {

    var $_store_mod;
    var $_coupon_mod;
    var $_couponsn_mod;

    function __construct() {
        $this->CouponApp();
    }

    function CouponApp() {
        parent::__construct();
        $this->_store_mod = & m('store');
        $this->_coupon_mod = & m('coupon');
        $this->_couponsn_mod = & m('couponsn');
    }

    function index() {
        $page = $this->_get_page(10);
        $store_list = $this->_coupon_mod->find(array(
            'fields' => 'coupon.*,s.store_name',
            'conditions' => 'coupon.if_issue = 1 AND coupon.end_time > ' . gmtime() . ' GROUP BY coupon.store_id',
            'join' => 'belong_to_store',
            'limit' => $page['limit'],
        ));


        $page['item_count'] = count($this->_coupon_mod->find(array(
                    'conditions' => 'coupon.if_issue = 1 AND coupon.end_time > ' . gmtime() . ' GROUP BY coupon.store_id',
                    'join' => 'belong_to_store',
        )));
        $this->_format_page($page);
        $this->assign('page_info', $page);

        foreach ($store_list as $key => $store) {
            $coupons = $this->_coupon_mod->find(array(
                'fields' => 'coupon.*,s.store_name',
                'conditions' => 'if_issue = 1 AND coupon.end_time > ' . gmtime() . ' AND coupon.store_id=' . $store['store_id'],
                'limit' => 5,
                'join' => 'belong_to_store',
            ));
            foreach($coupons as $k_coupon => $v_coupon){
                if(!$v_coupon['coupon_bg']){
                    $coupons[$k_coupon]['coupon_bg']=Conf::get('default_coupon_image');
                }
            }
            $store_list[$key]['coupons'] = $coupons;
        }


        $this->assign('store_list', $store_list);

        /* 当前位置 */
        $_curlocal = array(
            array(
                'text' => Lang::get('index'),
                'url' => 'index.php',
            ),
            array(
                'text' => Lang::get('coupon'),
                'url' => '',
            ),
        );
        $this->assign('_curlocal', $_curlocal);
        $this->_config_seo('title', ' 优惠券列表 - ' . Conf::get('site_title'));
        $this->display('coupon.index.html');
    }

    function view() {
        $coupon_id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        if (!$coupon_id) {
            $this->show_warning("error");
            exit;
        }
        $coupon = $this->_coupon_mod->get(array('conditions' => 'coupon_id = ' . $coupon_id));
        if (empty($coupon)) {
            $this->show_warning("error");
            exit;
        }

        //获取可领取的优惠券
        $sql1 = "SELECT COUNT(*) FROM {$this->_couponsn_mod->table} WHERE user_id = 0 AND remain_times>0 AND coupon_id=" . $coupon['coupon_id'];
        $coupon['not_received'] = $this->_couponsn_mod->getOne($sql1);
        $sql2 = "SELECT COUNT(*) FROM {$this->_couponsn_mod->table} WHERE user_id != 0 AND coupon_id=" . $coupon['coupon_id'];
        $coupon['hava_received'] = $this->_couponsn_mod->getOne($sql2);

        $url = SITE_URL . '/mobile/index.php?app=coupon%26act=view%26id=' . $coupon['coupon_id'];
        $coupon['scan_code'] = '<img src=' . SITE_URL . '/mobile/index.php?app=qrcode&url=' . $url . '/>';

        $this->assign('coupon', $coupon);

        $store = $this->_store_mod->get($coupon['store_id']);
        $this->assign('store', $store);


        /* 当前位置 */
        $_curlocal = array(
            array(
                'text' => Lang::get('index'),
                'url' => 'index.php',
            ),
            array(
                'text' => Lang::get('coupon'),
                'url' => 'index.php?app=coupon',
            ),
            array(
                'text' => $coupon['coupon_name'],
                'url' => '',
            ),
        );
        $this->assign('_curlocal', $_curlocal);
        $this->_config_seo('title', $coupon['coupon_name'].' - ' . Conf::get('site_title'));
        $this->display('coupon.view.html');
    }

}

?>
